<?php
class ItemController extends AppController
{
	var $name = 'Item';
	var $helpers = array('Text');
	var $paginate = array(
			'limit' => 15,
			'conditions' => array('city' => '北京'),
			'order' => array('Item.created' => 'desc')
		);

	function index()
	{
		$this->redirect("/");
		//$this->set('title_for_layout','悦淘会 - 每天都有优惠');
		//$this->set('items', $this->paginate('Item'));
	}
	
	function latest()
	{
		$this->set('title_for_layout','悦淘会 - 每天都有优惠');
		$this->set('items', $this->paginate('Item'));
	}

	function view($id = null)
	{
		if (empty($id))
		{
			$id = $this->params['id'];
		}
		$this->Item->id = $id;
		$item = $this->Item->read();
		$this->set('title_for_layout',$item['Item']['title'] . ' - 悦淘会 - 每天都有优惠');
		$this->set('item', $item);
		$this->set('referralCode', $this->Item->Supplier->field('referral_code'));
	}

	function add()
	{
		if(!empty($this->data))
		{
			if ($this->Item->save($this->data))
			{
				$this->redirect(array('controller' => 'item', 'action' => 'view', $this->Item->id));
			}
		}
	}

	function edit($id = null)
	{
		$this->Item->id = $id;
		if (empty($this->data))
		{
			$this->data = $this->Item->read();
		}
		else
		{
			if ($this->Item->save($this->data))
			{
				$this->Session->setFlash('updated');
				$this->redirect(array('controller' => 'item', 'action' => 'view', $this->Item->id));
			}
		}
	}

	function recommended($count)
	{
		if (empty($count)) $count = 1;
		$options = array(
			'conditions' => array(
				'Item.recommended' => 1, 
				'Item.end_time >' => time()+14*3600
			),
			'order' => 'Item.created desc',
			'limit' => $count
		);
		return $this->Item->find('all', $options);
	}

	function topLatest($count)
	{
		if (empty($count)) $count = 15;
		$options = array(
			'conditions' => array(
				'Item.end_time >' => time()+14*3600,
				'Item.recommended' => 0,
				'Item.city' => '北京'
			),
			'order' => 'Item.created desc',
			'limit' => $count 
		);
		return $this->Item->find('all', $options);
	}
	
	function recommendIt($id)
	{
	}

	function syncHao123()
	{
		$dataUrl = 'http://app.hao123.com/tuangou/data/tgdata.js';
		try
		{
			$data = file_get_contents($dataUrl);
			$data = str_ireplace("var strTgData='", "",$data);
			$data = str_ireplace("';", "",$data);
			$data = explode('],"\u', $data);
			$bj = str_ireplace('{"\u5317\u4eac":', '', $data[0]) . ']';
			$bj = json_decode($bj);
			$newItem = array();
			$sql = "SET NAMES utf8;";
			foreach($bj as $bjitem)
			{
				$sql = $sql . "INSERT INTO items (title, cover_image_url, content, value, price, rebate, start_time, end_time, url, website, site_url, city, user_id, created) SELECT ";
				$sql = $sql . "'" . $bjitem->title . "',";
				$sql = $sql . "'" . $bjitem->image . "',";
				$sql = $sql . "'" . $bjitem->title . "',";
				$sql = $sql . "'" . $bjitem->value . "',";
				$sql = $sql . "'" . $bjitem->price . "',";
				$sql = $sql . "'" . $bjitem->rebate . "',";
				$sql = $sql . "'" . $bjitem->startTime . "',";
				$sql = $sql . "'" . $bjitem->endTime . "',";
				$sql = $sql . "'" . $bjitem->item_url . "',";
				$sql = $sql . "'" . $bjitem->website . "',";
				$sql = $sql . "'" . $bjitem->siteurl . "',";
				$sql = $sql . "'" . $bjitem->city . "',";
				$sql = $sql . "'10000',";
				$sql = $sql . "'0' FROM DUAL WHERE NOT EXISTS (SELECT id FROM items WHERE url = '" . $bjitem->item_url . "');";
/*
				$newItem = array(
						'Item' => array(
							'title' => $bjitem->title,
							'cover_image_url' => $bjitem->image,
							'content' => $bjitem->title,
							'value' => $bjitem->value,
							'price' => $bjitem->price,
							'rebate' => $bjitem->rebate,
							'start_time' => $bjitem->startTime,
							'end_time' => $bjitem->endTime,
							'url' => $bjitem->item_url,
							'website' => $bjitem->website,
							'site_url' => $bjitem->siteurl,
							'city' => $bjitem->city,
							'user_id' => 10000,
							'recommended' => false
						)
				);
				$this->Item->save($newItem);
				$this->Item->create();
*/
			}
			$this->Item->query($sql);
		}
		catch(Exception $ex)
		{
			echo $ex->getMessage();
		}
	}
}
?>
